package cn.web;
import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import news.Comment;
import news.CommentDao;
import news.News;
import news.NewsDao;

public class NewsServlet extends HttpServlet {

	public NewsServlet() {
		super();
	}

	public void destroy() {
		super.destroy();
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		long nid=48;
		try{
		nid=Long.parseLong(request.getParameter("nid"));
		}catch(Exception e){}	
		News n=NewsDao.DAO.get(nid);
/*		if(n==null){
			out.print("<script>alert('不存在此新闻！');window.location.href='index.jsp';</script>");
			return;
		}*/
		List<Comment> c=CommentDao.DAO.findComments(nid);
		String ip = request.getRemoteAddr();
		request.setAttribute("news", n);
		request.setAttribute("comments",c);
		request.setAttribute("ip", ip);
		request.getRequestDispatcher("/WEB-INF/jsp/news.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

	public void init() throws ServletException {
		super.init();
	}
}